﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CloudStorageLight.Core
{

    /// <summary>
    /// Blobファイルオブジェクト
    /// </summary>
    public class BlobFile : BlobItem
    {
        public const string keyCheckoutuserid = "checkoutuserid";
        const string keyLockOwner = "LockOwner";
        const string keyOpaqueLock = "OpaqueLock";
        const string keyLockTimeout = "LockTimeout";
        const string keyFullTextEntryDate = "FullTextEntryDate";

        /// <summary>
        /// チェックアウトされているか？
        /// </summary>
        public bool IsCheckOut
        {
            get 
            {
                return !string.IsNullOrEmpty(CheckOutUserId);                 
            }
        }

        /// <summary>
        /// チェックアウトしているユーザ
        /// </summary>
        public string CheckOutUserId
        {
            get { return Metadata.ContainsKey(keyCheckoutuserid)  ? Metadata[keyCheckoutuserid] : null; }
            set 
            { 
                Metadata[keyCheckoutuserid] = value; 
            }
        }

        /// <summary>
        /// チェックアウトロックID
        /// </summary>
        public string OpaqueLock
        {
            get { return Metadata.ContainsKey(keyOpaqueLock) ? Metadata[keyOpaqueLock] : null; }
            set
            {
                Metadata[keyOpaqueLock] = value;
            }
        }

        public string LockOwner
        {
            get { return Metadata.ContainsKey(keyLockOwner) ? Metadata[keyLockOwner] : null; }
            set
            {
                Metadata[keyLockOwner] = value;
            }
        }

        /// <summary>
        /// チェックアウトロックID
        /// </summary>
        public DateTimeOffset? LockTimeout
        {
            get 
            { 
                if( Metadata.ContainsKey(keyLockTimeout))
                {
                    DateTimeOffset work;
                    if (DateTimeOffset.TryParseExact(Metadata[keyLockTimeout], "R", null, System.Globalization.DateTimeStyles.None, out work))
                    {
                        return work;
                    }
                }
                return null; 
            }
            set 
            {
                if (value == null)
                {
                    Metadata[keyLockTimeout] = null;
                }
                else
                {
                    Metadata[keyLockTimeout] = value.Value.ToString("R"); 
 
                }
            }
        }



        /// <summary>
        /// 全文検索登録
        /// </summary>
        public DateTimeOffset? FullTextEntryDate
        {
            get 
            { 
                if( Metadata.ContainsKey(keyFullTextEntryDate))
                {
                    DateTimeOffset work;
                    if (DateTimeOffset.TryParseExact(Metadata[keyFullTextEntryDate], "R", null, System.Globalization.DateTimeStyles.None, out work))
                    {
                        return work;
                    }
                }
                return null; 
            }
            set 
            {
                if (value == null)
                {
                    Metadata[keyFullTextEntryDate] = null;
                }
                else
                {
                    Metadata[keyFullTextEntryDate] = value.Value.ToString("R"); 
 
                }
            }

        }

        public BlobFile(BlobContainer container, string path)
            : base(container, path)
        {
        }


        /// <summary>
        /// アップロード
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public async Task UploadAsync(Stream stream)
        {
            await Container.UploadAsync(this, stream);
        }

        /// <summary>
        /// アップロード
        /// </summary>
        /// <param name="localpath"></param>
        /// <returns></returns>
        public async Task UploadAsync(string localpath)
        {
            await Container.UploadAsync(this, localpath); 
        }

        /// <summary>
        /// ダウンロード
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public async Task DownloadAsync(Stream stream)
        {
            await Container.DownloadAsync(this, stream);
        }

        /// <summary>
        /// ダウンロード
        /// </summary>
        /// <param name="localpath"></param>
        /// <returns></returns>
        public async Task DownloadAsync(string localpath)
        {
            await Container.DownloadAsync(this, localpath);
        }

        public override async Task GetMetadataAsync(bool fetch = true)
        {
            await base.GetMetadataAsync(fetch);

            if (Metadata.ContainsKey(keyLockTimeout))
            {
                if (LockTimeout.HasValue && LockTimeout.Value < DateTimeOffset.UtcNow)
                {
                    LockTimeout = null;
                    OpaqueLock = null;
                    CheckOutUserId = null;
                    await SetMetadataAsync();
                }
            }
        }

    }
}
